<?php
/**
 * 零起飞-(07FLY-CRM)
 * ==============================================
 * 版权所有 2015-2028   成都零起飞网络，并保留所有权利。
 * 网站地址: http://www.07fly.xyz
 * ----------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ==============================================
 * Author: kfrs <goodkfrs@QQ.com> 574249366
 * Date: 2019-10-3
 */

namespace app\ten\logic;

/**
 * 租户类型模块逻辑
 */
class TenTenantType extends TenBase
{

    /**
     * 租户类型列表
     */
    public function getTenTenantTypeList($where = [], $field = true, $order = 'sort asc', $paginate = DB_LIST_ROWS)
    {
        $list = $this->modelTenTenantType->getList($where, $field, $order, $paginate);
        foreach ($list as &$row){
            $moduleIdsName=$this->modelTenModule->getColumn(['id'=>['in',$row['module_ids']]], 'name');
            $row['module_ids_name']=arr2str($moduleIdsName);
        }
        return $list;
    }

    /**
     * 租户类型添加
     */
    public function tenTenantTypeAdd($data = [])
    {
        $validate_result = $this->validateTenTenantType->scene('add')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateTenTenantType->getError()];
        }
        $url = url('show');
        $result = $this->modelTenTenantType->setInfo($data);
        $result && action_log('新增', '新增租户类型，name：' . $data['name']);
        return $result ? [RESULT_SUCCESS, '添加成功', $url] : [RESULT_ERROR, $this->modelTenTenantType->getError()];
    }

    /**
     * 租户类型编辑
     */
    public function tenTenantTypeEdit($data = [])
    {
        $validate_result = $this->validateTenTenantType->scene('edit')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateTenTenantType->getError()];
        }
        $url = url('show');
        $result = $this->modelTenTenantType->setInfo($data);
        $result && action_log('编辑', '编辑租户模块，name：' . $data['name']);
        return $result ? [RESULT_SUCCESS, '编辑成功', $url] : [RESULT_ERROR, $this->modelTenTenantType->getError()];
    }

    /**
     * 租户类型删除
     */
    public function tenTenantTypeDel($where = [])
    {
        $result = $this->modelTenTenantType->deleteInfo($where, true);
        $result && action_log('删除', '删除租户类型，where：' . http_build_query($where));
        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelTenTenantType->getError()];
    }

    /**
     * 租户类型信息
     */
    public function getTenTenantTypeInfo($where = [], $field = true)
    {
        $info = $this->modelTenTenantType->getInfo($where, $field);
        if (!empty($info)) {
            $info['module_list'] = $this->modelTenTenantTypeModule->getColumn(['ten_tenant_type_id' => $info['id']], 'ten_module_id');
        }
        return $info;
    }

    /**
     * 租户类型菜单授权
     */
    public function setTenTenantTypeModules($data = [])
    {
        if (empty($data['module_ids']) || empty($data['id'])) {
            throw_response_error('参数错误');
        }
        $moduleIds = $data['module_ids'];
        $updata = [];
        foreach ($moduleIds as $moduleId) {
            $updata[] = ['ten_tenant_type_id' => $data['id'], 'ten_module_id' => $moduleId];
        }
        $updata && $this->modelTenTenantTypeModule->deleteInfo(['ten_tenant_type_id'=>$data['id']], true);

        $result = $this->modelTenTenantTypeModule->setList($updata);

        $result && $this->modelTenTenantType->setFieldValue(['id'=>$data['id']], 'module_ids', arr2str($moduleIds));

        $url = url('show');
        return $result ? [RESULT_SUCCESS, '设置成功', $url] : [RESULT_ERROR, $this->modelTenTenantType->getError()];
    }

    /**
     * 已经选择租户模块
     */
    public function selectTenantTypeList($module_list = [], $user_auth_list = [])
    {
        $user_auth_ids = array_extract($user_auth_list, 'sys_auth_id');
        foreach ($module_list as &$info) {
            in_array($info['id'], $user_auth_ids) ? $info['tag'] = 'active' : $info['tag'] = '';
        }
        return $module_list;
    }
}
